function elementRootDisplay(config = {
  e: null, className: '', show: false, breakClass: ''
}) {
  elementDisplay(config)
}

function elementArrayDisplay(config = {
  e: null, className: '', show: false, breakClass: ''
}) {
  config = config || {}
  config.isArray = true
  elementDisplay(config)
}

function elementDisplay(config = {
  e: null,
  className: '', show: false, breakClass: '', isArray: false,
}) {
  if (!config) config || {}
  e = config.e || null
  className = config.className || ''
  show = config.show || false
  breakClass = config.breakClass || ''
  isArray = config.isArray || false

  if (!className) return

  if (e && breakClass) {
    const pathArray = e.path || []
    for (let i = 0; i < pathArray.length; i++) {
      const div = pathArray[i] || {}
      const currentClassName = div.className || ''
      if (!currentClassName) {
        continue
      } else if (currentClassName === 'page' || currentClassName === 'sidebar') {
        break
      } else if (currentClassName && breakClass && currentClassName === breakClass) {
        return
      }
    }
  }
  const wxIconDisplay = show ? 'inherit' : 'none'
  _getLevelClass([className], wxIconDisplay).then(wxNode => {
  })
}

function elementLevelDisplay(config = {
  e: null,
  classNames: [], show: false, breakClass: '', isArray: false,
}) {
  if (!config) config || {}
  e = config.e || null
  classNames = config.classNames || []
  show = config.show || false
  breakClass = config.breakClass || ''
  isArray = config.isArray || false

  if (!classNames) return
  if (!Array.isArray(classNames)) return
  if (classNames.length === 0) return

  if (e && breakClass) {
    const pathArray = e.path || []
    for (let i = 0; i < pathArray.length; i++) {
      const div = pathArray[i] || {}
      const currentClassName = div.className || ''
      if (!currentClassName) {
        continue
      } else if (currentClassName === 'page' || currentClassName === 'sidebar') {
        break
      } else if (currentClassName && breakClass && currentClassName === breakClass) {
        return
      }
    }
  }

  const wxIconDisplay = show ? 'inherit' : 'none'
  _getLevelClass(classNames, wxIconDisplay).then(wxNode => {
  })
}

function _getLevelClass(classNames, display) {
  return new Promise((resolve, reject) => {
    const w = window ? window : {}
    const doc = w.document || null
    if (!doc) {
      reject(new Error('document = nil'))
    }
    if (!classNames) return
    if (!Array.isArray(classNames)) return
    if (!classNames.length) return
    const newClassArray = JSON.parse(JSON.stringify(classNames))
    const newClass = newClassArray.splice(0, 1)[0]
    const element = doc.getElementsByClassName(newClass) || []
    if (element) {
      const len = element.length
      for (let i = 0; i < len; i++) {
        const wxNode = element[i]
        const currentClassName = wxNode.className
        const currentClassArr = currentClassName.split(' ')
        if (currentClassArr.indexOf(newClass) >= 0) {
          if (newClassArray.length) {
            return _getLevelClass(newClassArray)
          } else if (wxNode && wxNode.nodeName && wxNode.nodeName === 'DIV') {
            wxNode.style.display = display
            resolve(true)
          }
        }
      }
    }
  })
}


function elementIncludeClassDisplay(config = {
  e: null,
  classNames: [], show: false, breakClass: '', isArray: false,
}) {
  if (!config) config || {}
  e = config.e || null
  classNames = config.classNames || []
  show = config.show || false
  breakClass = config.breakClass || ''
  isArray = config.isArray || false

  if (!classNames) return
  if (!Array.isArray(classNames)) return
  if (classNames.length === 0) return

  if (e && breakClass) {
    const pathArray = e.path || []
    for (let i = 0; i < pathArray.length; i++) {
      const div = pathArray[i] || {}
      const currentClassName = div.className || ''
      if (!currentClassName) {
        continue
      } else if (currentClassName === 'page' || currentClassName === 'sidebar') {
        break
      } else if (currentClassName && breakClass && currentClassName === breakClass) {
        return
      }
    }
  }

  const wxIconDisplay = show ? 'inherit' : 'none'
  _getIncludeClass(classNames, wxIconDisplay).then(wxNode => {
  })
}


function _getIncludeClass(classNames, display) {
  return new Promise((resolve, reject) => {
    const w = window ? window : {}
    const doc = w.document || null
    if (!doc) {
      reject(new Error('document = nil'))
    }
    if (!classNames) return
    if (!Array.isArray(classNames)) return
    if (!classNames.length) return

    const newClassArray = JSON.parse(JSON.stringify(classNames))
    const newClass = newClassArray[0]
    const element = doc.getElementsByClassName(newClass) || []
    if (element) {
      const len = element.length
      for (let i = 0; i < len; i++) {
        const wxNode = element[i]
        const currentClassName = wxNode.className
        const currentClassArr = currentClassName.split(' ')
        let enable = true
        for(let j = 0; j < newClassArray.length; j++) {
          const temp = newClassArray[j]
          if (currentClassArr.indexOf(temp) === -1) {
            enable = false
            break
          }
        }
        if (enable) {
          if (wxNode && wxNode.nodeName && wxNode.nodeName === 'DIV') {
            wxNode.style.display = display
            resolve(true)
          }
        }
      }
    }
  })
}

module.exports = {
  elementRootDisplay,
  elementArrayDisplay,
  elementDisplay,
  elementLevelDisplay,
  elementIncludeClassDisplay,
}